# 更新时间2024-8-21
# 更新内容添加检测结果的打印输出



import re
import openpyxl
import datetime
import os
from PySide6.QtCore import QThread, Signal
from Components.Parameter import *
import getpass
from openpyxl.utils import get_column_letter
from openpyxl.styles import PatternFill, colors

colorwarning = PatternFill("solid", fgColor="FFD700")
colorerror = PatternFill("solid", fgColor="FF4500")


class Parser:
    def __init__(self):
        super().__init__()
        self.excel = './ISP6sAWBPatameter.xlsx'
    # def setExcelpath(self, path):
    #
    def setSavepath(self, path):
        self.save = path
    def setcpppath(self, path):
        self.path = path
    def getAWBParameterlist(self, path):
        with open(path, 'r', encoding='utf-8') as f:
            content = f.read()

        content = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', content)
        # 正则表达式移除单行注释 // ...
        content = re.sub(re.compile(r'//.*?\n'), '\n', content)
        stack = []
        result = []
        current = []
        for char in content:
            if char == '{':
                stack.append(current)
                current = []
            elif char == '}':
                if current:
                    result.append(''.join(current))
                if stack:
                    last = stack.pop()
                    last.extend(current)
                    current = last
            else:
                current.append(char)
        # print('【成功】AWB文件解析成功!')
        return result
    def insertcell(self, cell, val):
        if cell.comment:
            text = cell.comment.text
            if ',' in text:
                if val not in range(int(text.split(',')[0]), int(text.split(',')[1]) + 1):
                    cell.fill = colorwarning
                    self.error += 1
            else:
                if val != int(text):
                    cell.fill = colorwarning
                    self.error += 1
        cell.value = val

    def writesheet0(self, sheet, res, sheet_dict):
        idx = 1
        for key, value in sheet_dict.items():
            IndexValue = int(res[value[0]].split(',')[value[1]].replace(" ", "").replace('\n', ''))
            sheet[get_column_letter(4) + str(idx)] = IndexValue
            # Predictor Gain
            if 'Spatial_L' in key:
                if value[1] == 0:
                    r = int(res[39].split(',')[0].replace(" ", "").replace('\n', ''))
                    if IndexValue not in range(int(r * 0.95), int(r * 1.05)):
                        sheet[get_column_letter(4) + str(idx)].fill = colorwarning
                elif value[1] == 1:
                    if IndexValue != 512:
                        sheet[get_column_letter(4) + str(idx)].fill = colorwarning
                elif value[1] == 2:
                    b = int(res[39].split(',')[2].replace(" ", "").replace('\n', ''))
                    if IndexValue not in range(int(b * 0.95), int(b * 1.05)):
                        sheet[get_column_letter(4) + str(idx)].fill = colorwarning
            elif 'Spatial_H' in key:
                if value[1] == 0:
                    r = int(res[38].split(',')[0].replace(" ", "").replace('\n', ''))
                    if IndexValue not in range(int(r * 0.95), int(r * 1.05)):
                        sheet[get_column_letter(4) + str(idx)].fill = colorwarning
                elif value[1] == 1:
                    if IndexValue != 512:
                        sheet[get_column_letter(4) + str(idx)].fill = colorwarning
                elif value[1] == 2:
                    b = int(res[38].split(',')[2].replace(" ", "").replace('\n', ''))
                    if IndexValue not in range(int(b * 0.95), int(b * 1.05)):
                        sheet[get_column_letter(4) + str(idx)].fill = colorwarning

            # Unit and Golden gain
            if 'UnitGain' in key or 'GoldenGain' in key or 'UnitValue' in key or 'GoldenValue' in key:
                if int(IndexValue) != 0:
                    sheet[get_column_letter(4) + str(idx)].fill = colorwarning

            idx += 1

    def insertParameter(self):
        self.error = 0
        try:
            self.wb = openpyxl.load_workbook(self.excel)
        except:
            print('【错误】模板文件不存在，请检查')
            return
        # print(f'【成功】模板文件读取成功!')

        sheet_names = self.wb.sheetnames
        res = self.getAWBParameterlist(self.path)
        # sheet_0
        self.writesheet0(self.wb[sheet_names[0]], res, sheet0_dict)
        # print(f'【成功】AWB Calibration Sheet 写入成功!')

        # sheet_1
        row = 4
        for left in [86, 110, 134, 158, 182, 206, 230, 254]:
            col = 2
            for idx in range(left, left + 23):
                for rgb in range(3):
                    value = [idx, rgb]
                    IndexVal = res[value[0]].split(',')[value[1]].replace(" ", "").replace('\n', '')
                    self.insertcell(self.wb[sheet_names[1]][get_column_letter(col) + str(row)], int(IndexVal))
                    col += 1
            row += 1

        # AWB LV threshold for spatial predictor
        row = 31
        col = 2
        for idx in range(279, 288):
            l = res[idx].split(',')[0].replace(" ", "").replace('\n', '')
            r = res[idx].split(',')[1].replace(" ", "").replace('\n', '')
            self.insertcell(self.wb[sheet_names[1]][get_column_letter(col) + str(row)], int(l))
            self.insertcell(self.wb[sheet_names[1]][get_column_letter(col + 1) + str(row)], int(r))
            col += 2

        # AWB number threshold for temporal predictor
        row = 16
        col = 2
        for i in range(19):
            val = res[290].split(',')[i].replace(" ", "").replace('\n', '')
            self.insertcell(self.wb[sheet_names[1]][get_column_letter(col) + str(row)], int(val))
            col += 1
        self.insertcell(self.wb[sheet_names[1]][get_column_letter(2) + str(14)],
                        int(res[291].split(',')[0].replace(" ", "").replace('\n', '')))

        # Reliable parent block threshold
        row = 18
        col = 2
        for i in range(19):
            val = res[292].split(',')[i].replace(" ", "").replace('\n', '')
            self.insertcell(self.wb[sheet_names[1]][get_column_letter(col) + str(row)], int(val))
            col += 1

        # Noise Reduction
        row = 20
        for idx in range(293, 301):
            col = 2
            for i in range(19):
                val = res[idx].split(',')[i].replace(" ", "").replace('\n', '')
                self.insertcell(self.wb[sheet_names[1]][get_column_letter(col) + str(row)], int(val))
                col += 1
            row += 1


        # sheet_2
        # sunset
        col = 2
        row = 2
        for i in range(8):
            val = res[304].split(',')[i].replace(" ", "").replace('\n', '')
            self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(val))
            row += 1
        # subwindow
        row = 3
        for idx in [307, 310]:
            col = 6
            vallist = res[idx].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for i in range(9):
                val = a[i]
                self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(val))
                col += 1
            row += 1


        # Extra
        row = 13
        idx = 319
        for i in range(16):
            col = 3
            vallist = res[idx].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for i in range(30):
                val = a[i]
                self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(val))
                col += 1
            row += 1
            idx += 9

        # Exclude
        row = 33
        idx = 459
        for i in range(8):
            col = 3
            vallist = res[idx].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for i in range(11):
                val = a[i]
                self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(val))
                col += 1
            row += 1
            idx += 4


        # SkyColor
        row = 3
        col = 18
        idx = 503
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(27):
            val = a[i]
            self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(val))
            col += 1

        # Non-neutral probability for spatial and temporal weighting look-up table
        vallist = res[506].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[2]][get_column_letter(3) + str(43)], int(a[0]))
        self.insertcell(self.wb[sheet_names[2]][get_column_letter(3) + str(43)], int(a[1]))
        col = 2
        row = 46
        for i in range(19):
            self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], a[i + 2])
            col += 1

        # AWB daylight locus probability
        row = 51
        idx = 507
        for i in range(8):
            col = 3
            vallist = res[idx].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for i in range(23):
                val = a[i]
                self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(val))
                col += 1
            row += 1
            idx += 2
        # AWB Light source probability (P1)
        row = 62
        idx = 524
        for i in range(8):
            col = 3
            vallist = res[idx].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for i in range(23):
                val = a[i]
                self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(val))
                col += 1
            row += 1
            idx += 2

        # AWB convergence parameter
        row = 73
        col = 3
        idx = 541
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row + 1)], int(a[1]))
        self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row + 2)], a[2])

        # Offset threshold
        row = 80
        col = 3
        idx = 542
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row + 1)], int(a[1]))
        self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row + 2)], int(a[2]))

        # Offset ratio LV
        row = 84
        idx = 543
        for i in range(3):
            vallist = res[idx].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            col = 3
            for i in range(2):
                val = a[i]
                self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(val))
                col += 2
            row += 1
            idx += 1
        # AWB daylight locus target Offset ratio LUT
        row = 89
        idx = 547
        for i in range(3):
            vallist = res[idx].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            col = 3
            for i in range(21):
                val = a[i]
                self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(val))
                col += 1
            row += 1
            idx += 2
        # Daylight locus offset LUTs
        row = 93
        idx = 554
        for idx in [554, 555, 557, 558, 560, 561]:
            vallist = res[idx].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            col = 3
            for i in range(21):
                val = a[i]
                self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(val))
                col += 1
            row += 1
        # AWB Green//Magenta offset threshold
        row1 = 101
        row2 = 106
        for idx in [566, 569, 572]:
            col = 3
            vallist = res[idx].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for i in range(43):
                val = a[i]
                if i == 0:
                    self.insertcell(self.wb[sheet_names[2]][get_column_letter(3) + str(row1)], int(val))
                    row1 += 1
                else:
                    self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row2)], int(val))
                    col += 1
                    if i == 21:
                        col = 3
                        row2 += 1
            row2 += 1
        # AWB light source weight LUT
        row = 121
        for idx in [574, 575, 577, 578, 580, 581]:
            col = 3
            vallist = res[idx].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for i in range(11):
                val = a[i]
                self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(val))
                col += 1
            row += 1
        # AWB light source preference gain LUT ---daylight locus offset
        row = 113
        for idx in [584, 631, 678]:
            col = 3
            for i in range(21):
                vallist = res[idx].replace('\n', '').split(',')
                a = []
                for v in vallist:
                    if v.strip() != '':
                        a.append(v.strip())
                self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(a[0]))
                self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row + 1)], int(a[1]))
                col += 1
                idx += 1

            row += 2
        # AWB light source preference gain LUT ---magenta green locus offset
        row = 128
        for idx in [606, 618, 653, 665, 700, 712]:
            col = 3
            for i in range(11):
                vallist = res[idx].replace('\n', '').split(',')
                a = []
                for v in vallist:
                    if v.strip() != '':
                        a.append(v.strip())
                self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row)], int(a[0]))
                self.insertcell(self.wb[sheet_names[2]][get_column_letter(col) + str(row + 1)], int(a[1]))
                col += 1
                idx += 1
            row += 2

        # sheet_3
        # AWB Refinement
        idx = 728
        row = 2
        col = 2
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(4):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1
        row = 7
        for i in range(4, 12):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            col += 1

        # P0 Stability
        idx = 729
        col = 12
        row = 2
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(4):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1

        idx = 730
        col = 16
        row = 2
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(5):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1

        # Preference Color Stability
        idx = 732
        col = 20
        row = 2
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(4):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1

        # StatLimit
        idx = 747
        row = 10
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 1)], int(a[1]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 2)], int(a[2]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 7) + str(row + 2)], int(a[3]))


        idx = 735
        row = 14
        for i in range(3):
            col = 3
            vallist = res[idx].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for y in range(8):
                self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[y]))
                col += 2
            row += 1
            idx += 1

        idx = 747
        row = 17
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(28, 44):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            col += 1
        # FaceCommon
        # Factor
        idx = 748
        row = 26
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))

        # LandmarkCVThr
        idx = 749
        row = 28
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))

        # LandmarkProb
        idx = 750
        row = 29
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))

        # DistVarThr
        idx = 751
        row = 30
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 2) + str(row)], int(a[2]))

        # FeatureCVProb
        idx = 752
        row = 31
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 2) + str(row)], int(a[2]))

        # DiffThr
        idx = 753
        row = 27
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))

        # Mode
        idx = 754
        row = 25
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))

        # FD_RGB_Bound
        idx = 755
        row = 32
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))

        # TempoSmooth
        idx = 756
        row = 33
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 2) + str(row)], int(a[2]))
        # SceneChange
        idx = 757
        row = 34
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))

        # StatAvoidFaceArea FaceWinRatio CentralWeight LandmarkWeight
        idx = 758
        row = 21
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 1)], int(a[1]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 2)], int(a[2]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 3)], int(a[3]))

        # RefHS_Low RefHS_Mid RefHS_High
        idx = 760
        row = 25
        col = 12
        for i in range(24):
            vallist = res[idx + i].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))
            col += 2
            if i in [2 + i * 3 for i in range(8)]:
                row += 1
                col = 12

        # SceneJudge
        idx = 793
        row = 34
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(8):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            col += 1

        # RefTarget
        idx = 792
        row = 23
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(3):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            col += 2

        # TempoWeight
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(12) + str(33)], int(a[-1]))

        # TOL
        idx = 794
        row = 38
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 3) + str(row)], int(a[1]))

        # RestrictRatio_TOL
        idx = 795
        row = 39
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 3) + str(row)], int(a[1]))

        # LV-LowCT
        idx = 797
        row = 40
        for i in range(4):
            col = 12
            vallist = res[idx + i].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for x in range(4):
                self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[x]))
                col += 1
            row += 1

        #  Undersat
        idx = 802
        row = 44
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for x in range(6):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[x]))
            col += 1

        #  GainRatioRestrictLUT
        idx = 803
        row = 47
        col = 10
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for x in range(23):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[x]))
            col += 1

        #  GainRatioRestrictLUT
        idx = 804
        row = 49
        col = 14
        for i in range(19):
            vallist = res[idx + i].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 1)], int(a[1]))
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 2)], int(a[2]))
            col += 1

        # FaceSizeRatioLow ~ Prob0_Max
        idx = 824
        row = 53
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 1)], int(a[1]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 2)], int(a[2]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 3)], int(a[3]))

        # Prob1
        idx = 825
        row = 58
        for i in range(8):
            col = 14
            vallist = res[idx + i].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for i in range(19):
                self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
                col += 1
            row += 1
        # Prob2
        idx = 835
        row = 67
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(5):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1

        # Prob3
        idx = 836
        row = 67
        col = 15
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(5):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1

        # Prob3
        idx = 837
        row = 67
        col = 18
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(5):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1

        # P0
        idx = 838
        row = 74
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(4):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1

        # P1
        idx = 839
        row = 74
        col = 15
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(4):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1
        # LV
        idx = 840
        row = 78
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[-2]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 1)], int(a[-1]))
        # Portrait
        idx = 841
        row = 81
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 1)], int(a[1]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 2)], int(a[2]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 3)], int(a[3]))

        # Portrait
        idx = 842
        row = 87
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))

        # Portrait
        idx = 843
        row = 86
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 2)], int(a[1]))

        # 4Enable SatTargetEn
        idx = 844
        row = 21
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 1)], int(a[1]))

        # Face Assisted AWB
        idx = 845
        row = 96
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(8):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            col += 1

        # RefTargetThr
        idx = 846
        row = 101
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(3):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            col += 1
        # RefTargetThr
        row = 103
        for idx in [850, 854, 858, 862, 866, 870, 874, 878]:
            col = 12
            vallist = res[idx].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for i in range(6):
                self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
                col += 1
            row += 1
        # RefTarget - Reference Target Setting
        idx = 880
        row = 98
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 1)], int(a[1]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 2)], int(a[2]))

        # PrefGain for Predictor Gain
        row = 112
        idx = 881
        for i in range(8):
            col = 12
            vallist = res[idx + i].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))
            row += 1
        # Prob0 - Face Size
        row = 121
        idx = 890
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(4):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1

        # on-neutral P1 table for spatial and temporal weighting look-up table
        row = 127
        col = 12
        idx = 891
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(19):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            col += 1

        # on-neutral P1 table for spatial and temporal weighting look-up table
        row = 128
        idx = 893
        for i in range(8):
            col = 12
            vallist = res[idx + i].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for x in range(19):
                self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[x]))
                col += 1
            row += 1

        # Prob2
        row = 138
        for idx in [903, 904, 906, 907, 909, 910]:
            col = 12
            vallist = res[idx].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for x in range(11):
                self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[x]))
                col += 1
            row += 1

        # Prob3
        row = 145
        idx = 913
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(4):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1

        # Prob4
        row = 150
        idx = 914
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(4):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1
        # ConfThr
        row = 155
        idx = 915
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))

        # DistanceThr
        row = 156
        idx = 916
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 2) + str(row)], int(a[2]))

        # CompRatio
        row = 157
        idx = 916
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 2) + str(row)], int(a[2]))

        row = 158
        idx = 919
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))

        # AdvSetting
        row = 159
        idx = 920
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(6):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            col += 1
        # Enable
        row = 94
        idx = 921
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))

        # Smooth Statistic
        row = 162
        idx = 922
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(8):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            col += 1

        row = 161
        idx = 923
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 2)], int(a[9]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 3)], int(a[10]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 4)], int(a[11]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 5)], int(a[12]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 6)], int(a[13]))


        # Panel Damping Prevention --P0
        row = 174
        idx = 924
        col = 10
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(6):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            col += 2

        row = 174
        idx = 924
        col = 10
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(6):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            col += 2

        # Trigger
        row = 177
        idx = 925
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))

        # Scene __LV
        row = 176
        idx = 926
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row + 2)], int(a[-1]))

        # Gain
        row = 180
        idx = 927
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))

        # Speed
        row = 181
        idx = 928
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))

        # Strength
        row = 179
        idx = 929
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[3]][get_column_letter(col + 1) + str(row)], int(a[1]))

        # SatRatioCtrl
        row = 182
        idx = 930
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(4):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            col += 1

        # SatRatioCtrl
        row = 183
        idx = 931
        col = 12
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(6):
            self.insertcell(self.wb[sheet_names[3]][get_column_letter(col) + str(row)], int(a[i]))
            col += 1

        # sheet_3

        # Chip dependent parameter
        row = 2
        idx = 934
        col = 5
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(3):
            self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1

        # LvThld[3]
        row = 4
        idx = 935
        col = 11
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(3):
            self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row)], int(a[i]))
            col += 1

        # GammaLut[17]
        row = 5
        idx = 936
        col = 11
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(17):
            self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row)], int(a[i]))
            col += 1

        # WeightLut[3][17]
        row = 7
        idx = 937
        for i in range(3):
            col = 11
            vallist = res[idx + i].replace('\n', '').split(',')
            a = []
            for v in vallist:
                if v.strip() != '':
                    a.append(v.strip())
            for i in range(17):
                self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row)], int(a[i]))
                col += 1
            row += 1

        #
        row = 2
        idx = 941
        col = 11
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row + 1)], int(a[1]))
        self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row + 8)], int(a[-1]))

        # One-shot AWB parameter
        row = 8
        idx = 942
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row + 1)], int(a[1]))
        self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row + 2)], int(a[2]))

        # MWB parameter
        row = 14
        idx = 943
        col = 3
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row + 1)], int(a[1]))

        # MWB parameter
        row = 14
        idx = 943
        col = 8
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row + 1)], int(a[1]))

        #  Misc parameter
        row = 14
        idx = 945
        col = 8
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row)], int(a[0]))
        self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row + 1)], int(a[1]))
        self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row + 2)], int(a[2]))

        #  AWB tuning information
        row = 14
        idx = 946
        col = 13
        vallist = res[idx].replace('\n', '').split(',')
        a = []
        for v in vallist:
            if v.strip() != '':
                a.append(v.strip())
        for i in range(8):
            self.insertcell(self.wb[sheet_names[4]][get_column_letter(col) + str(row)], int(a[i]))
            row += 1


        # 打印结果
        row = 27
        col = 4
        now_str = datetime.datetime.now().strftime('%Y-%m-%d')
        username = getpass.getuser()
        self.wb[sheet_names[4]][get_column_letter(col) + str(row)] = now_str
        self.wb[sheet_names[4]][get_column_letter(col) + str(row + 2)] = username
        self.wb[sheet_names[4]][get_column_letter(col) + str(row + 4)] = f'{self.error}/1105'
        Accuracy_rate = round((1 - self.error/1105) * 100 , 4)
        if Accuracy_rate == 100:
            print(f'[成功]当前通道AWB正确率{Accuracy_rate}% ,{self.error}/1105')
        else:
            print(f'[警告]当前通道AWB正确率{Accuracy_rate}% ,{self.error}/1105')


    def wbsave(self):
        self.wb.save(os.path.join(self.save,'AWB.xlsx'))
    def run(self):
        try:
            self.insertParameter()
        except Exception as e:
            print(e)
        self.wbsave()
        # current_folder_name = os.path.basename(os.path.normpath(self.save))
        # print(f'[{current_folder_name}]:AWB Done')



